﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Settings
{
    /// <summary>
    /// This object represents the methods of a Setting.
    /// </summary>
    internal class SettingDAO
    {

        #region general
        private static Setting LoadObj(IDataReader datareader)
        {
            Setting obj = new Setting();
            obj.SettingID = MpDataHelp.GetInt(datareader, "SettingID");
            obj.SettingKey = MpDataHelp.GetString(datareader, "SettingKey");
            obj.LanguageID = MpDataHelp.GetInt(datareader, "LanguageID");
            obj.SettingValue = MpDataHelp.GetString(datareader, "SettingValue");
            obj.SettingGroup = MpDataHelp.GetString(datareader, "SettingGroup");
            obj.SettingType = MpDataHelp.GetInt(datareader, "SettingType");

            return obj;
        }
        #endregion

        public static int Setting_Add(string SettingKey, int LanguageID, string SettingValue, string SettingGroup, int? SettingType)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Setting_Add");
            db.AddOutParameter(dbCommand, "SettingID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "SettingKey", DbType.String, SettingKey);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            db.AddInParameter(dbCommand, "SettingValue", DbType.String, SettingValue);
            db.AddInParameter(dbCommand, "SettingGroup", DbType.String, SettingGroup);
            db.AddInParameter(dbCommand, "SettingType", DbType.Int32, SettingType);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@SettingID"));
            return -1;
        }
        public static int Setting_Update(Setting obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Setting_Update");
            db.AddInParameter(dbCommand, "SettingID", DbType.Int32, obj.SettingID);
            db.AddInParameter(dbCommand, "SettingKey", DbType.String, obj.SettingKey);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, obj.LanguageID);
            db.AddInParameter(dbCommand, "SettingValue", DbType.String, obj.SettingValue);
            db.AddInParameter(dbCommand, "SettingGroup", DbType.String, obj.SettingGroup);
            db.AddInParameter(dbCommand, "SettingType", DbType.Int32, obj.SettingType);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Setting_Delete(int SettingID, string SettingKey)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Setting_Delete");
            db.AddInParameter(dbCommand, "SettingID", DbType.Int32, SettingID);
            db.AddInParameter(dbCommand, "SettingKey", DbType.String, SettingKey);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Setting> Setting_List(int SettingID, string SettingKey)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Setting_List");
            db.AddInParameter(dbCommand, "SettingID", DbType.Int32, SettingID);
            db.AddInParameter(dbCommand, "SettingKey", DbType.String, SettingKey);
            List<Setting> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Setting>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



